// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "merchant_id": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "所属商户",
    "label": "所属商户"
  },
  "sn": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "打印机编号SN",
    "label": "打印机编号SN"
  },
  "key": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "打印机识别码KEY",
    "label": "打印机识别码KEY"
  },
  "remark": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "打印机备注信息",
    "label": "打印机备注信息"
  },
  "print_times": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "总打印次数",
    "label": "总打印次数"
  },
  "success_times": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "成功打印次数",
    "label": "成功打印次数"
  },
  "error_times": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "失败打印次数",
    "label": "失败打印次数"
  },
  "work_status": {
    "rules": [
      {
        "format": "int"
      },
      {
        "range": [
          {
            "text":"未更新",
            "value":0
          },
          {
            "text":"离线异常",
            "value":1
          },
          {
            "text":"在线正常",
            "value":2
          },
          {
            "text":"在线不正常",
            "value":3
          }
        ]
      }
    ],
    "title": "打印机状态信息",
    "defaultValue": 0,
    "label": "打印机状态信息"
  },
}

const enumConverter = {
    'work_status_valuetotext':{
      "0": "未更新",
      "1": "离线异常",
      2:`在线正常`,
      3:`在线不正常`
    },
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
